package com.xdf.docmanagementsystem.common.util;

import com.xdf.docmanagementsystem.dao.DocumentMapper;
import com.xdf.docmanagementsystem.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * @program: doc-management-system
 * @description: 编号生成器
 * @author: Zhang
 * @create: 2020-06-23 17:53
 **/
@Component
public class CodeGenerateUtil {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DocumentMapper documentMapper;

    /**
     * 生成员工编号
     *
     * @return
     */
    public String generateEmployeeCode() {
        String prefix = "R";
        long count = employeeMapper.selectCountByExample(new Example().addWhereCondition(Example.whereCondition("DATE(create_time)=DATE(", ")", new Date())));
        StringBuilder sb = new StringBuilder()
                .append(prefix)
                .append(concatDateStr())
                .append(String.format("%04d", ++count));
        return sb.toString();
    }

    /**
     * 生成文档系统编号
     *
     * @return
     */
    public String generateDocumentCode() {
        String prefix = "W";
        long count = documentMapper.selectCountByExample(new Example().addWhereCondition(Example.whereCondition("DATE(create_time)=DATE(", ")", new Date())));
        StringBuilder sb = new StringBuilder()
                .append(prefix)
                .append(concatDateStr())
                .append(String.format("%04d", ++count));
        return sb.toString();
    }

    /**
     * 拼接日期时间字符
     *
     * @return
     */
    private String concatDateStr() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONDAY) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        StringBuilder sb = new StringBuilder()
                .append(year)
                .append(String.format("%02d", month))
                .append(String.format("%02d", day));
        return sb.toString();
    }
}
